package pck.ivrGraph.forms;

import org.jgraph.JGraph;
import org.jgraph.graph.*;
import pck.ivrGraph.models.JGModel;
import pck.ivrGraph.models.flows.IvrFlow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class MainGraphForm extends JFrame {

	private JGraph graph = null;

	private class MouseHandler extends MouseAdapter{
		public void mousePressed(MouseEvent e) {
			if(e.getClickCount() == 2){
				int x = e.getX(), y = e.getY();
				DefaultGraphCell cell = (DefaultGraphCell)graph.getFirstCellForLocation(x, y);
				if(cell != null){
					new EditFlowForm((IvrFlow)cell.getUserObject()); 
				}
			}
		}
	}

	public MainGraphForm(){
		super("IvrFlow Graph");
		GraphModel model = new DefaultGraphModel();
		GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory());
		graph = new JGraph(model, view);
		graph.setAntiAliased(true);
		graph.addMouseListener(new MouseHandler());

		try{
			UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
			SwingUtilities.updateComponentTreeUI(this);
			ArrayList<DefaultGraphCell> cells = new JGModel();
			graph.getGraphLayoutCache().insert(cells.toArray());
			this.setResizable(true);
			this.getContentPane().add(new JScrollPane(graph));
			this.pack();
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setExtendedState(Frame.MAXIMIZED_BOTH);
			this.setVisible(true);
		}catch (Exception e){
			new AlertForm(e);
		}
	}
}
